
import env from "./env"
import { setUserInfo, clearUserInfo } from './store';

//Request类
class Request {
    //构造
    constructor(params) {
        this.awbUrl = env.awbUrl
    }
    //get请求
    get(url, data) {
        return this.request('GET', url, data)
    }
    //post请求
    post(url, data,errCallback) {
        return this.request('POST', url, data,errCallback)
    }

    //后台post请求
    postWithBackground(url, data,errCallBack ) {
        return this.requestWithBackground('POST', url, data,errCallBack )
    }

    requestWithBackground(method, url, data,errCallBack) {
        const vm = this
        // 封装请求头
        const newUrl = vm.awbUrl + url;
        const header ={
            'Content-Type': 'application/json' // 请求头
        };
        data={
            "timestamp": new Date().getTime(),
            "body":data,
            "appId": "DP-MINI",
            "accessToken": "YzJjLWFwaS0yMDI0LWRldi1WSU5EQTpjMmMtYXBpLTIwMjQtZGV2LVZJTkRB",
            "requestId": new Date().getTime()
        }
        console.log("请求接口",newUrl)
        console.log("请求入参",data)
        //封装Promise调用
        return new Promise((resolve, reject) => {
            wx.request({
                url: newUrl,
                data,
                method,
                header,
                async success(res) {
                    console.log('请求返回结果',res.data)
                    if (res.data.code!=200){
                        errCallBack()
                        return
                    }else {

                    }


                    return resolve(res.data)
                },
                fail(err) {
                    console.log(err || '服务器发生错误')
                    reject({
                        msg: '请求失败',
                        url: newUrl,
                        method,
                        data,
                        err
                    })
                },
                complete(e) {
                }
            })
        })
    }


    //发送请求
    request(method, url, data,errCallBack) {
        const vm = this
        // 封装请求头
        const newUrl = vm.awbUrl + url;
        const header ={
            'Content-Type': 'application/json' // 请求头
        };
        data={
            "timestamp": new Date().getTime(),
            "body":data,
            "appId": "DP-MINI",
            "accessToken": "YzJjLWFwaS0yMDI0LWRldi1WSU5EQTpjMmMtYXBpLTIwMjQtZGV2LVZJTkRB",
            "requestId": new Date().getTime()
        }
        wx.showLoading({
            title: '加载中...',
            mask: true, // 是否显示透明蒙层，防止触摸穿透
        })

        console.log("请求接口",newUrl)
        console.log("请求入参",data)


        //封装Promise调用
        return new Promise((resolve, reject) => {
            wx.request({
                url: newUrl,
                data,
                method,
                header,
                async success(res) {
                    console.log('请求返回结果',res.data)
                    const { data: { code, msg } } = res;
                    if (code!= 200) {
                        wx.showModal({
                            title: msg || '服务器发生错误',
                            icon: 'none',
                            showCancel: false
                        });
                        if (errCallBack){
                            errCallBack()
                        }
                        return
                    }
                    return resolve(res.data)

                },
                fail(err) {
                    reject({
                        msg: '请求失败',
                        url: newUrl,
                        method,
                        data,
                        err
                    })
                },
                complete(e) {
                    wx.hideLoading();

                }
            })
        })
    }
}
const api = new Request({
})

module.exports = api